package com.ms.controller.basic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ms.dao.entity.StaffmemberEty;
import com.ms.dao.mapper.StaffmemberDao;

@Controller
@RequestMapping(value="/basic/LoginController/")
public class LoginController {
	
	private Logger logger = Logger.getLogger(LoginController.class);
	
	@Autowired
	private StaffmemberDao staffmemberDao;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="login.action")
	public @ResponseBody String login(HttpServletRequest request, StaffmemberEty userEty) throws Exception {
		JSONObject retObj = new JSONObject();
		List dataList = staffmemberDao.selectByEntity(userEty);
		if(dataList.size() == 1) {
			request.getSession().setAttribute("UserEty", dataList.get(0));
			retObj.put("result", "success");
		} else {
			retObj.put("result", "fail");
		}
		retObj.put("success", true);
		return retObj.toString();
	}
	
	@ExceptionHandler
	public @ResponseBody String handle(Exception e) {
		logger.error(e.getMessage(), e);
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		obj.put("result","error");
		obj.put("info",e.getMessage());
		return obj.toString();
	}
}
